#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N = 1e6 + 10;
int n, a[N], f[N][2], f1[N];
vector<int> q[N];
/*
3 5 2 5 1 2 1 4
1 1 2 2 3 4 5 5

5 3 7 2 4 13 11 6 5 5 3 5 12 8 13
0
*/
signed main(){
    freopen("color.in", "r", stdin);
    freopen("color.out", "w", stdout);
    // ios::sync_with_stdio(false);
    // cin.tie(0);cout.tie(0);
    int t;
    cin >> t;
    int maxn = 0;
    while(t--){
        cin >> n;
        for(int i = 1; i <= maxn; i++){
            q[a[i]].clear();
        }
        maxn = 0;
        for(int i = 1; i <= n; i++){
            cin >> a[i];
            q[a[i]].push_back(i);
            maxn = max(maxn, a[i]);
        }
        for(int i = 2; i <= n; i++){
            f[i][1] = max(f[i - 1][1], f[i - 1][0]) + (a[i] == a[i - 1]) * a[i];
            f[i][0] = max(f[i - 1][1], f[i - 1][0]);
            for(auto j : q[a[i]]){
                if(j >= i)break;
                f[i][0] = max(f[i][0], f[j + 1][0] + a[i]);
            }
         //   cout << i << " " << f[i][0] << " " << f[i][1] << endl;
        }
        cout << max(f[n][1], f[n][0]) << endl;
    }
}